home *** CD-ROM | disk | FTP | other *** search
- import java.io.IOException;
- import java.util.Random;
- import java.util.Vector;
- import javax.microedition.lcdui.Alert;
- import javax.microedition.lcdui.AlertType;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.List;
-
- public class GameCanvas extends Canvas implements Runnable, CommandListener {
- private Display display = null;
- private List mainMenu = null;
- private Command quitC = null;
- private Alert endGame = null;
- private final int MAX_MISSILES = 7;
- private final int MAX_BALLOONS = 7;
- private final int MAX_INDEX = 6;
- private final int WIDTH = ((Canvas)this).getWidth();
- private final int HEIGHT = ((Canvas)this).getHeight();
- private final int[] balloonX = new int[]{2, 16, 30, 44, 58, 72, 87};
- private final int BALLOON_DELAY = 30;
- private Sprite copter = null;
- private Sprite truck = null;
- private AutoSprite missile = null;
- private AutoSprite balloon = null;
- private AutoSprite box = null;
- private Image balloonImg = null;
- private Image boxImg = null;
- private Image missileImg = null;
- private SpriteVector missileVector = null;
- private SpriteVector balloonVector = null;
- private SpriteVector boxVector = null;
- private volatile Thread animationThread = null;
- private boolean moveCopter = true;
- private boolean moveTruck = false;
- private int missilesFired = 0;
- private int balloonLaunched = 0;
- private int balloonDestroyed = 0;
- private int boxCaught = 0;
- private int balloonCreateDelay = 30;
-
- public GameCanvas(Display var1, List var2) {
- this.display = var1;
- this.mainMenu = var2;
- this.quitC = new Command("Quit", 4, 1);
- ((Displayable)this).addCommand(this.quitC);
- ((Displayable)this).setCommandListener(this);
- this.missileVector = new SpriteVector();
- this.balloonVector = new SpriteVector();
- this.boxVector = new SpriteVector();
- this.copter = new Sprite(this.WIDTH, this.HEIGHT);
- this.truck = new Sprite(this.WIDTH, this.HEIGHT);
-
- try {
- this.balloonImg = Image.createImage("/balloon.png");
- this.missileImg = Image.createImage("/bomb.png");
- this.boxImg = Image.createImage("/box.png");
- } catch (IOException var4) {
- ((Throwable)var4).printStackTrace();
- }
-
- this.initCopter();
- this.initTruck();
- }
-
- private void boxHitTruck(SpriteVector var1) {
- for(int var3 = 0; var3 < ((Vector)var1).size(); ++var3) {
- AutoSprite var2 = (AutoSprite)((Vector)var1).elementAt(var3);
- if (var2.getY() >= this.truck.getY() + 2 && var2.getX() >= this.truck.getX() && var2.getX() <= this.truck.getX() + 11) {
- ++this.boxCaught;
- var2.setState(1);
- }
- }
-
- }
-
- public void commandAction(Command var1, Displayable var2) {
- if (var1 == this.quitC) {
- this.stop();
- System.gc();
- this.display.setCurrent(this.mainMenu);
- }
-
- }
-
- private void draw(Graphics var1) {
- this.balloonVector.draw(var1);
- this.missileVector.draw(var1);
- this.copter.draw(var1);
- this.boxVector.draw(var1);
- this.truck.draw(var1);
- }
-
- private void initBalloon() {
- Random var1 = new Random(System.currentTimeMillis());
- Image[] var2 = new Image[1];
- int[][] var3 = new int[][]{new int[1]};
- int var4 = Math.abs(var1.nextInt()) % 6;
- this.balloon.setX(this.balloonX[var4]);
- this.balloon.setY(this.HEIGHT - 11 - 13);
- this.balloon.setDy(1);
- this.balloon.setAnimationDelay(10);
- var2[0] = this.balloonImg;
- this.balloon.setImage(var2);
- this.balloon.setSequenceFrame(1, 1, var3);
- this.balloon.setIndex(0, 0);
- }
-
- private void initCopter() {
- Image[] var1 = new Image[2];
- int[][] var2 = new int[][]{{0, 1}};
- this.copter.setX(0);
- this.copter.setY(0);
- this.copter.setDx(2);
- this.copter.setDy(0);
-
- try {
- var1[0] = Image.createImage("/heli_left.png");
- var1[1] = Image.createImage("/heli_right.png");
- } catch (IOException var4) {
- ((Throwable)var4).printStackTrace();
- }
-
- this.copter.setImage(var1);
- this.copter.setSequenceFrame(1, 2, var2);
- this.copter.setIndex(0, 0);
- }
-
- private void initMissile() {
- Image[] var1 = new Image[1];
- int[][] var2 = new int[][]{new int[1]};
- byte var3 = 0;
- byte var4 = 7;
- if (this.copter.getIndex() == 0) {
- var3 = 5;
- } else if (this.copter.getIndex() == 1) {
- var3 = 8;
- }
-
- this.missile.setX(this.copter.getX() + var3);
- this.missile.setY(this.copter.getY() + var4);
- this.missile.setDy(2);
- this.missile.setAnimationDelay(1);
- var1[0] = this.missileImg;
- this.missile.setImage(var1);
- this.missile.setSequenceFrame(1, 1, var2);
- this.missile.setIndex(0, 0);
- }
-
- private void initTruck() {
- Image[] var1 = new Image[2];
- int[][] var2 = new int[][]{{0, 1}};
- this.truck.setX(0);
- this.truck.setY(this.HEIGHT - 11);
- this.truck.setDx(2);
- this.truck.setDy(0);
-
- try {
- var1[0] = Image.createImage("/truck_left.png");
- var1[1] = Image.createImage("/truck_right.png");
- } catch (IOException var4) {
- ((Throwable)var4).printStackTrace();
- }
-
- this.truck.setImage(var1);
- this.truck.setSequenceFrame(1, 2, var2);
- this.truck.setIndex(0, 0);
- }
-
- public void keyPressed(int var1) {
- if (((Canvas)this).getGameAction(var1) == 6) {
- this.moveCopter = false;
- this.moveTruck = true;
- } else if (((Canvas)this).getGameAction(var1) == 1) {
- this.moveTruck = false;
- this.moveCopter = true;
- } else if (this.moveCopter) {
- this.copter.gameActionPressed(((Canvas)this).getGameAction(var1), true);
- } else if (this.moveTruck) {
- this.truck.gameActionPressed(((Canvas)this).getGameAction(var1), true);
- }
-
- }
-
- public void keyReleased(int var1) {
- this.copter.gameActionPressed(((Canvas)this).getGameAction(var1), false);
- this.truck.gameActionPressed(((Canvas)this).getGameAction(var1), false);
- }
-
- private void missileHitBalloon(SpriteVector var1, SpriteVector var2) {
- for(int var5 = 0; var5 < ((Vector)var1).size(); ++var5) {
- AutoSprite var3 = (AutoSprite)((Vector)var1).elementAt(var5);
-
- for(int var6 = 0; var6 < ((Vector)var2).size(); ++var6) {
- AutoSprite var4 = (AutoSprite)((Vector)var2).elementAt(var6);
- if (var3.getY() + 5 >= var4.getY() && var3.getX() + 1 >= var4.getX() && var3.getX() <= var4.getX() + 7) {
- ++this.balloonDestroyed;
- var3.setState(1);
- var4.setState(1);
- this.box = new AutoSprite(this.WIDTH, this.HEIGHT - 7, "box");
- Image[] var7 = new Image[1];
- int[][] var8 = new int[][]{new int[1]};
- this.box.setX(var4.getX() + 2);
- this.box.setY(var4.getY() + 9);
- this.box.setDx(0);
- this.box.setDy(2);
- this.box.setAnimationDelay(1);
- var7[0] = this.boxImg;
- this.box.setImage(var7);
- this.box.setSequenceFrame(1, 1, var8);
- this.box.setIndex(0, 0);
- this.boxVector.addElement(this.box);
- }
- }
- }
-
- }
-
- public void paint(Graphics var1) {
- var1.setColor(16777215);
- var1.fillRect(0, 0, this.WIDTH, this.HEIGHT);
- this.draw(var1);
- }
-
- public void run() {
- byte var1 = 100;
- Thread var2 = Thread.currentThread();
-
- try {
- while(var2 == this.animationThread) {
- long var3 = System.currentTimeMillis();
- this.tick();
- ((Canvas)this).repaint();
- ((Canvas)this).serviceRepaints();
- long var5 = System.currentTimeMillis() - var3;
- if (var5 < (long)var1) {
- synchronized(this){}
-
- try {
- this.wait((long)var1 - var5);
- } catch (Throwable var10) {
- throw var10;
- }
- }
- }
- } catch (InterruptedException var11) {
- }
-
- }
-
- synchronized void start() {
- this.animationThread = new Thread(this);
- this.animationThread.start();
- }
-
- synchronized void stop() {
- this.animationThread = null;
- }
-
- private void tick() {
- this.copter.tick();
- this.truck.tick();
- if (this.copter.hasFired() && this.missilesFired != 7) {
- this.missile = new AutoSprite(this.WIDTH, this.HEIGHT, "missile");
- this.initMissile();
- this.missileVector.addElement(this.missile);
- ++this.missilesFired;
- }
-
- this.missileVector.tick();
- if (this.balloonLaunched != 7) {
- if (this.balloonCreateDelay == 30) {
- ++this.balloonLaunched;
- this.balloon = new AutoSprite(this.WIDTH, 5, "balloon");
- this.initBalloon();
- this.balloonVector.addElement(this.balloon);
- this.balloonCreateDelay = 0;
- }
-
- ++this.balloonCreateDelay;
- }
-
- this.balloonVector.tick();
- this.missileHitBalloon(this.missileVector, this.balloonVector);
- this.boxVector.tick();
- this.boxHitTruck(this.boxVector);
- if (this.balloonVector.isEmpty() && this.boxVector.isEmpty()) {
- this.stop();
- StringBuffer var1 = new StringBuffer("Missiles\nFired: " + Integer.toString(this.missilesFired) + '\n' + "Balloons\nLaunched: " + Integer.toString(this.balloonLaunched) + '\n' + "Balloons\nDestroyed: " + Integer.toString(this.balloonDestroyed) + '\n' + "Boxes\nCaught:" + Integer.toString(this.boxCaught));
- this.endGame = new Alert("Results", var1.toString(), (Image)null, (AlertType)null);
- this.endGame.setTimeout(-2);
- this.display.setCurrent(this.endGame, this.mainMenu);
- this.endGame = null;
- Object var2 = null;
- }
-
- }
- }
-